package org.ipower.financial.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.ipower.financial.dao.IDao;
/**
 * 基础数据库操作实现类。
 * @author young.
 * @since 2013-08-18.
 * */
public class DaoImpl<T> implements IDao<T> {
	private SessionFactory sessionFactory;
	
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		if(this.sessionFactory != null){
			return this.sessionFactory.getCurrentSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T load(Class<T> c, Serializable id) {
		if(c != null && id != null && this.getCurrentSession() != null){
			return (T)this.getCurrentSession().get(c, id);
		}
		return null;
	}

	@Override
	public void delete(T o) {
		if(o != null && this.getCurrentSession() != null){
			this.getCurrentSession().delete(o);
		}
	}

	@Override
	public void save(T o) {
		if(o != null && this.getCurrentSession() != null){
			this.getCurrentSession().save(o);
		}
	}

	@Override
	public void update(T o) {
		if(o != null && this.getCurrentSession() != null){
			this.getCurrentSession().update(o);
		}
	}

	@Override
	public void saveOrUpdate(T o) {
		if(o != null && this.getCurrentSession() != null){
			this.getCurrentSession().saveOrUpdate(o);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> find(String hql, Map<String, Object> params, Integer page,Integer rows) {
		if(hql != null && !hql.isEmpty() && this.getCurrentSession() != null){
			Query query = this.getCurrentSession().createQuery(hql);
			if(query != null){
				//参数
				if(params != null && params.size() > 0){
					for(String key:params.keySet()){
						if(key != null && !key.isEmpty()){
							query.setParameter(key, params.get(key));
						}
					}
				}
				//分页
				if(page == null && rows == null){//不分页
					return query.list();					
				}else{
					if(page < 1) page = 1;
					if(rows < 1) rows = 10;
					return query.setFirstResult((page - 1) * rows)
							.setMaxResults(rows)
							.list();					
				}
			}
		}
		return null;
	}

	@Override
	public Long count(String hql, Map<String, Object> params) {
		if(hql != null && !hql.isEmpty() && this.getCurrentSession() != null){
			Query query = this.getCurrentSession().createQuery(hql);
			if(query != null){
				//参数
				if(params != null && params.size() > 0){
					for(String key:params.keySet()){
						if(key != null && !key.isEmpty()){
							query.setParameter(key, params.get(key));
						}
					}
				}
				return (Long)query.uniqueResult();
			}
		}
		return null;
	}

	@Override
	public Integer executeHql(String hql, Map<String, Object> params) {
		if(hql != null && !hql.isEmpty() && this.getCurrentSession() != null){
			Query query = this.getCurrentSession().createQuery(hql);
			if(query != null){
				//参数
				if(params != null && params.size() > 0){
					for(String key:params.keySet()){
						if(key != null && !key.isEmpty()){
							query.setParameter(key, params.get(key));
						}
					}
				}
				return query.executeUpdate();
			}
		}
		return null;
	}

}